package com.shujia.share;


public class ShareDemo2 extends Thread {

    private NumberCount numberCount;


    public ShareDemo2(String name, NumberCount numberCount) {
        super(name);// 设置线程名称
        this.numberCount = numberCount;
    }

    public static void main(String[] args) {
        // 创建共享资源
        NumberCount count = new NumberCount();

        Thread t1 = new ShareDemo2("线程1", count);
        Thread t2 = new ShareDemo2("线程2", count);

        t1.start();
        t2.start();

        // join 让主线程等待子线程执行完毕再继续执行
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(count.getCount());

    }


    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(getName() + ":" + numberCount.getCount());
            numberCount.incr();
        }
    }
}
